/**
 * Copyright: Copyright (c) 2012
 * Company:深圳市海乐淘电子商务有限公司
 * @author lifuyu
 * @date 2012-7-17 上午10:32:22
 * @version V1.0
 */
package com.microcm.system;

import java.io.UnsupportedEncodingException;
import java.util.List;

import com.microcm.common.CFUtils;
import com.microcm.common.CUtils;
import com.microcm.common.Final;
import com.microcm.common.SystemToolkit;
import com.microcm.system.cache.SystemCacheKeeper;
import com.microcm.system.entity.SessionUnit;
import com.microcm.system.entity.SysMenu;
import com.microcm.system.entity.TagMenu;
import com.microcmframework.core.action.BeanAction;

/**
 * 服务数据类
 */
public class SrvData extends BeanAction
{
	private SystemCacheKeeper systemCacheKeeper;

	public SrvData()
	{
		super("system");
		this.systemCacheKeeper = (SystemCacheKeeper) SystemToolkit.getBean("systemCacheKeeper");
	}

	/**
	 * 获取当前管理用户的ID
	 * 
	 * @author：lifuyu
	 * @return
	 */
	public String getCurrentSuperId()
	{
		SessionUnit su = (SessionUnit) this.getSessionVal(Final.ONLINE_MGR_FLAG);
		if (su == null || !CUtils.isUID(su.getUid()))
			return null;
		return su.getUid();
	}

	/**
	 * 获取用户的ID
	 * 
	 * @author：lifuyu
	 * @return
	 */
	public String getCurrentUserId()
	{
		SessionUnit su = (SessionUnit) this.getSessionVal(Final.ONLINE_USER_FLAG);
		if (su == null || !CUtils.isUID(su.getUid()))
			return null;
		return su.getUid();
	}

	/** ***【网站导航菜单、BANNER广告】********************************* */
	/**
	 * 返回网站的导航菜单
	 * 
	 * @return
	 */
	public List<?> getNavMenu()
	{
		return (List<?>) this.getPData("_getNavMenu", new IGPData()
		{
			public Object gen()
			{
				return systemCacheKeeper.getMenuByPmnid(0);
			}
		});
	}

	/**
	 * 合成URL
	 * 
	 * @author kansum
	 */
	private String combinUrl(String url, int mnid, String keyword)
	{
		if (url == null)
			return "";
		int p = url.indexOf(Final.MARK_FOR_CATEGORY);
		if (p != -1)
			url = url.replaceAll("\\" + Final.MARK_FOR_CATEGORY, String.valueOf(mnid));
		p = url.indexOf(Final.MARK_FOR_KEYWORD);
		if (p != -1)
		{
			try
			{
				url = url.replaceAll("\\" + Final.MARK_FOR_KEYWORD, CFUtils.urlEncode(keyword));
			}
			catch (UnsupportedEncodingException e)
			{
			}
		}
		return url;
	}

	/**
	 * 格式化菜单的超链接
	 * 
	 * @author kansum
	 */
	private String fmtMenuLink(SysMenu menu, String target)
	{
		StringBuffer html = new StringBuffer();
		if (menu.getMtype() == 0)
			html.append("<a href='" + combinUrl(getCtxurl() + menu.getPageurl(), menu.getMnid(), "") + "' title='" + menu.getName() + "' " + target + ">" + menu.getName() + "</a>");
		else if (menu.getMtype() == 1)
			html.append("<a href='" + combinUrl(menu.getPageurl(), 0, "") + "' title='" + menu.getName() + "' " + target + ">" + menu.getName() + "</a>");
		else if (menu.getMtype() == 2)
			html.append("<a href='javascript:void()' onclick=\"" + menu.getPageurl() + "\" title='" + menu.getName() + "'>" + menu.getName() + "</a>");
		return html.toString();
	}

	/**
	 * 返回导航菜单HTML
	 * 
	 * @author kansum
	 */
	public String getNavMenuHtml()
	{
		return (String) this.getPData("_getNavMenuHtml", new IGPData()
		{
			public Object gen()
			{
				StringBuffer html = new StringBuffer("<li><a href='" + getCtxurl() + "/'>首页</a></li> ");
				List<SysMenu> menuLs = systemCacheKeeper.getMenuByPmnid(0);
				for (SysMenu menu : menuLs)
				{
					html.append("<li>").append(fmtMenuLink(menu, "")).append("</li> ");
				}
				return html.toString();
			}
		});
	}

	/**
	 * 返回导航类目HTML
	 * 
	 * @author kansum
	 */
	public String getAllNavCategoryHtml()
	{
		return (String) this.getPData("getAllNavCategoryHtml", new IGPData()
		{
			public Object gen()
			{
				StringBuffer html = new StringBuffer("");
				List<SysMenu> menuLs = systemCacheKeeper.getMenuByPmnid(0);
				for (SysMenu menu : menuLs)
				{
					html.append("<div class='category-item tal'> ");
					html.append("<h4>" + fmtMenuLink(menu, "target='_blank'") + "</h4> ");
					html.append("<ul class='clr'> ");

					List<SysMenu> subMenuLs = systemCacheKeeper.getMenuByPmnid(menu.getMnid());
					if (CUtils.isNotEmpty(subMenuLs))
					{
						for (SysMenu subMenu : subMenuLs)
						{
							html.append("<li> ");
							html.append("<h5>" + fmtMenuLink(subMenu, "target='_blank'") + "</h5> ");
							html.append("<span>");

							List<TagMenu> tagLs = systemCacheKeeper.getTagByMnid(subMenu.getMnid(), true);
							if (CUtils.isNotEmpty(tagLs))
							{
								for (TagMenu tag : tagLs)
								{
									html.append("<a href='" + combinUrl(getCtxurl() + "/svl/search?mnid=$C&q=$Q", subMenu.getMnid(), tag.getName()) + "' target='_blank'>" + tag.getName() + "</a> ");
								}
							}

							html.append("</span>");
							html.append("</li> ");
						}
					}
					html.append("</ul>");
					html.append("</div> ");
				}
				return html.toString();
			}
		});
	}

	/**
	 * 返回查询goodsList页面显示的菜单导航类目HTML
	 * 
	 * @author kansum
	 */
	public String getNavCategoryHtml()
	{
		return (String) this.getPData("getNavCategoryHtml", new IGPData()
		{
			public Object gen()
			{
				int mnid = CUtils.isInteger(getRequestVal("mnid")) ? Integer.parseInt(getRequestVal("mnid")) : 0;
				if (mnid < 1)
					return "";

				StringBuffer html = new StringBuffer("");

				html.append("<ul class='clr'> ");

				List<SysMenu> subMenuLs = systemCacheKeeper.getMenuByPmnid(mnid);
				if (CUtils.isNotEmpty(subMenuLs))
				{
					for (SysMenu subMenu : subMenuLs)
					{
						html.append("<li> ");
						html.append("<h5>" + fmtMenuLink(subMenu, "target='_blank'") + "</h5> ");
						html.append("<span>");

						List<TagMenu> tagLs = systemCacheKeeper.getTagByMnid(subMenu.getMnid(), false);
						if (CUtils.isNotEmpty(tagLs))
						{
							for (TagMenu tag : tagLs)
							{
								html.append("<a href='" + combinUrl(getCtxurl() + "/svl/search?mnid=$C&q=$Q", subMenu.getMnid(), tag.getName()) + "' target='_blank'>" + tag.getName() + "</a> ");
							}
						}

						html.append("</span>");
						html.append("</li> ");
					}
				}
				html.append("</ul>");

				return html.toString();
			}
		});
	}

	/**
	 * 返回查询subGoodsList页面显示的菜单导航类目HTML
	 * 
	 * @author kansum
	 */
	public String getSubNavCategoryHtml()
	{
		return (String) this.getPData("getSubNavCategoryHtml", new IGPData()
		{
			public Object gen()
			{
				int mnid = CUtils.isInteger(getRequestVal("mnid")) ? Integer.parseInt(getRequestVal("mnid")) : 0;
				if (mnid < 1)
					return "";

				StringBuffer html = new StringBuffer("");

				html.append("<ul class='clr'> ");
				html.append("<li> ");

				SysMenu sysMenu = systemCacheKeeper.getMenuByMnid(mnid);
				if (CUtils.isNotEmpty(sysMenu))
				{
					html.append("<h5>" + fmtMenuLink(sysMenu, "target='_blank'") + "</h5> ");
				}
				html.append("<span>");
				List<TagMenu> tagLs = systemCacheKeeper.getTagByMnid(mnid, false);
				if (CUtils.isNotEmpty(tagLs))
				{
					for (TagMenu tag : tagLs)
					{
						html.append("<a href='" + combinUrl(getCtxurl() + "/svl/search?mnid=$C&q=$Q", mnid, tag.getName()) + "' target='_blank'>" + tag.getName() + "</a> ");
					}
				}
				html.append("</span>");
				html.append("</li> ");
				html.append("</ul>");

				return html.toString();
			}
		});
	}

	/**
	 * 返回查询的关键字导航
	 * 
	 * @author kansum
	 */
	public String getSearchNavHtml()
	{
		return (String) this.getPData("getSearchNavHtml", new IGPData()
		{
			public Object gen()
			{
				StringBuffer html = new StringBuffer("");

				html.append("<ul class='clr'> ");
				html.append("<li> 搜索关键字： ");

				int mnid = CUtils.isInteger(getRequestVal("mnid")) ? Integer.parseInt(getRequestVal("mnid")) : 0;
				if (mnid > 0)
				{
					SysMenu sysMenu = systemCacheKeeper.getMenuByMnid(mnid);
					if (CUtils.isNotEmpty(sysMenu))
					{
						html.append("<h5>" + fmtMenuLink(sysMenu, "target='_blank'") + "</h5> ");
					}
				}
				html.append("<span class='kw'>");
				try
				{
					html.append(CFUtils.urlDecode(getRequestVal("q")));
				}
				catch (UnsupportedEncodingException e)
				{
				}
				html.append("</span>");
				html.append("</li> ");
				html.append("</ul>");

				return html.toString();
			}
		});
	}

	/**
	 * 查询结果分页
	 * 
	 * @return
	 */
	public String getNavPage()
	{
		return (String) this.getPData("getNavPage", new IGPData()
		{
			public Object gen()
			{

				int totalPage = (Integer) getRequestAttrVal("totalPage");
				if (totalPage < 2)
					return "";

				int spanPage = 7;
				String mnid = getRequestVal("mnid");
				int page = (Integer) getRequestAttrVal("page");
				String sort = getRequestVal("idx");

				int pageFrom = ((page - 1) / spanPage) * spanPage + 1;
				int pageEnd = pageFrom + spanPage - 1;
				if (pageEnd > totalPage)
					pageEnd = totalPage;

				StringBuffer buff = new StringBuffer();
				String preUrl = "?mnid=" + mnid + "&sort=" + sort + "&page=";
				int i = pageFrom;
				if (i > spanPage)
					buff.append("<a href=\"" + preUrl + (i - 1) + "\">...</a>");
				for (; i <= pageEnd; i++)
				{
					if (page == i)
						buff.append("<strong>" + i + "</strong>");
					else
						buff.append("<a href=\"" + preUrl + i + "\">" + i + "</a>");
				}
				if (i < totalPage)
					buff.append("<a href=\"" + preUrl + i + "\">...</a>");
				return buff.toString();
			}
		});
	}
}